/**
 * Procesadores de Lenguaje, curso 2009-2010
 * @author grupo 02
 *
 */

package compilador;

import java.io.ObjectInputStream;
import java.io.FileInputStream;
import java.io.IOException;

import interprete.MaquinaP;
import util.Instruction;

public class Interprete {

    private static MaquinaP mP;


    private static void uso(){
        System.out.println("Uso:");
        System.out.println("Interprete <programa a ejecutar> -n | -t");
        System.out.println("-n Ejecucion modo normal");
        System.out.println("-t Ejecucion modo traza");
    }


    private static void intro(){
        System.out.println("\nInterprete Maquina P.");
        System.out.println("Procesadores de Lenguaje, curso 2009-2010 ");
        System.out.println("Grupo numero 02\n");
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        boolean traza=false;
        mP = new MaquinaP();
        intro();
        if (args.length != 2){
            System.out.println("Numero de argumentos incorrecto");
            uso();
            System.exit(-1);
        }


        if (args[1].equals("-t"))
            //traza
            traza=true;
        else if (args[1].equals("-n"))
            //normal
            traza=false;
        else{
            System.out.println("argumento incorrecto");
            uso();
            System.exit(-1);
        }

        mP.inicia(traza);

        try{

            FileInputStream fis = new FileInputStream(args[0]);
            ObjectInputStream in = new ObjectInputStream(fis);
            Instruction inst = (Instruction)in.readObject();
            try{
            while (true){
                mP.loadInstruction(inst);
                inst = (Instruction)in.readObject();
            }
            }
            catch(java.io.EOFException ex){
            in.close();

            //Ejecutar
            mP.run();
            //Debug
            //System.out.println(mP.printProg());
            }
        }catch(IOException ex){
            ex.printStackTrace();
        }catch(ClassNotFoundException ex){
            ex.printStackTrace();
        }catch(Exception ex) {
            ex.printStackTrace();
        }

    }

}
